﻿using DramaEnglish.WPF.ViewModels;
using HandyControl.Controls;
using IceCreamMachine.EventAggregators;
using IceCreamMachine.Services;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace IceCreamMachine.ViewModels
{
    class RawMaterialViewModel : ViewModelBase
    {

        private int _strawberry = 100;
        public int Strawberry
        {
            get { return _strawberry; }
            set { SetProperty(ref _strawberry, value); }
        }

        private int _chocolates = 100;
        public int Chocolates
        {
            get { return _chocolates; }
            set { SetProperty(ref _chocolates, value); }
        }

        private int _iceCream = 100;
        public int IceCream
        {
            get { return _iceCream; }
            set { SetProperty(ref _iceCream, value); }
        }

        private int _leftIceCream = 100;
        public int LeftIceCream
        {
            get { return _leftIceCream; }
            set { SetProperty(ref _leftIceCream, value); }
        }

        private static readonly object Lock1 = new object();
        private static readonly object Lock2 = new object();
        private static readonly object Lock = new object();
        public RawMaterialViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator ea)
           : base(regionManager, dialogService, ea)
        {
            _ = EventAggregator.GetEvent<PubSubEvent<EnumMaterial>>().Subscribe((x) =>
            {
                if (x.Equals(EnumMaterial.Strawberry))
                {
                    Strawberry--;
                }
                else if (x.Equals(EnumMaterial.IceCream))
                {
                    IceCream--;
                }
                else if (x.Equals(EnumMaterial.Chocolates))
                {
                    Chocolates--;
                }
                var min = IceCream < Chocolates ? IceCream : Chocolates;
                LeftIceCream = min < Strawberry ? min : Strawberry;
                if (min<10)
                {
                    Growl.Info("该换材料了");
                }
            },
ThreadOption.PublisherThread, false);
        }

        /// <summary>
        /// 加材料锁定资源
        /// </summary>
        public DelegateCommand AddRawMaterialCommand => new(() =>
        {
            try
            {
               
                Task.Run(() => {
                    lock (Lock1)
                    {
                        lock (Lock1)
                        {
                            Task.Delay(1000 * 5).Wait();
                            IceCream = 100;
                            Chocolates=100;
                            Strawberry = 100;
                            EventAggregator.GetEvent<PubSubEvent<EnumMaterial>>().Publish(EnumMaterial.None);
                            
                        }
                    }
                });
            }
            catch (System.Exception ex)
            {
                Growl.Error(ex.Message);
            }
           
        });
    }

}